<template>
    <div class="treeview">
        <tree-item v-for="item in model" :key="item.nodeKey" :model="item"></tree-item>
    </div>
</template>
<script>
    import TreeItem from './node.vue';
    import { findComponentsDownward } from '@/utils/assist';
    export default {
        name: 'TreeView',
        props: {
            model: {
                type: Array,
                default() {
                    return [];
                }
            }
        },
        components: {
            TreeItem
        },
        mounted() {
            // node选中
            this.$on('selected', ori => {
                const nodes = findComponentsDownward(this, 'TreeItem');
                nodes.forEach(node => {
                    this.$set(node.model, 'selected', false);
                });
                this.$set(ori, 'selected', true);
                this.$emit('on-selected', ori);
            });
            // node展开
            this.$on('toggle-expand', (model) => {
                this.$emit('on-toggle-expand', model);
            });
            // dropdown展开
            this.$on('dropdown-expand', (model) => {
                this.$emit('on-dropdown-expand', model);
            });
            // dropdown项点击
            this.$on('dropdown-click', (index) => {
                this.$emit('on-dropdown-click', index);
            });
        },
        methods: {

        }
    }
</script>